#include <iostream>
#include <string>
#include <mysql.h>
#include <stdlib.h>
#include <unistd.h>

// 用于解析从HTTP获取的信息
static bool CutString(const std::string &target, std::string &sub1_out, std::string &sub2_out, std::string sep)
{
    size_t pos = target.find(sep);
    if(pos != std::string::npos)
    {
        sub1_out = target.substr(0, pos);
        sub2_out = target.substr(pos + sep.size());
        return true;
    }
    return false; 
}

// 从HTTP获取信息
bool GetQueryString(std::string &query_string)
{
    bool result = false;
    //fd 0&1 已经重定向了
    // cerr << "_______________________________________" << endl;
    // cerr << "Debug CGITest METHOD: " << getenv("METHOD") << endl;
    std::string method = getenv("METHOD");
    if("GET" == method)
    {
        query_string = getenv("QUERY_STRING");
        // cerr << "Debug GET QUERY_STRING: " << query_string << endl;
        result = true;
    }
    else if("POST" == method)
    {
        // cerr << "Debug Content-Length: " << getenv("CONTENT_LENGTH") << endl;
        int content_length = atoi(getenv("CONTENT_LENGTH"));
        char c = 0;
        while(content_length--)
        {
            read(0, &c, 1);
            query_string.push_back(c);
        }
        // cerr << "Debug POST QUERY_STRING: " << query_string << endl;
        result = true;
    }
    else 
    {
        result = false;
    }
    return result;
}

bool InsertSql(std::string sql)
{
    MYSQL *conn = mysql_init(nullptr);  // 初始化mysql句柄
    if (nullptr == mysql_real_connect(conn, "127.0.0.1", "http_test", "123456", "http_db", 3306, nullptr, 0))   // 连接到MySQL
    {
        std::cerr << "connect error" << std::endl;
        return 1;
    }
    std::cerr << "connect success" << std::endl;
    std::cerr << "query: " << sql << std::endl;
    if(0 != mysql_query(conn, sql.c_str())) // 执行sql
    {
        std::cerr << "query error" << std::endl;
    }

    mysql_close(conn);  // 关闭连接
    return true;
}

int main()
{
    // std::cout << "version: " << mysql_get_client_info() << std::endl;    // mysql client vision
    
    std::string query_string;
    if(GetQueryString(query_string))    // 从http中获取
    {
        std::cerr << "Get sql from http: " << query_string << std::endl;
        // 数据处理
        // 形如name=yfy&passwd=123
        std::string name, passwd;
        CutString(query_string, name, passwd, "&"); // 变成name=yfy 和passwd=123，还需要继续拆分
        std::string tmp;    // 舍弃的前缀临时占用
        CutString(name, tmp, name, "=");
        CutString(passwd, tmp, passwd, "=");

        // 执行sql
        // std::string sql = "insert into user (name, password) values(\'yfy\', \'123\')";
        std::string sql = "insert into user (name, password) values(\'";
        sql += name;
        sql += "\', \'";
        sql += passwd;
        sql += "\')";      
        if(InsertSql(sql))
        {
            std::cout << "<html>";
            std::cout << "<head><meta charset=\"utf-8\"></head>";
            std::cout << "<body><h1>注册成功！</h1></body>";
        }
    }
    
    return 0;
}
